package wili.attrMapping.demo.xml;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import wili.c.Bean9;
import wili.c.Ca;
import wili.c.Class9a;
import wili.c.Str9a;
import wili.misc.Exhausters;

/**
 * 
 * @author weilai2@163.com
 *
 */
class XmlDemoGenerator {

	static class Bean {
		public String toString() {
			return Class9a.strOfBean(this, ", ");
		}

		public Bean() {

		}

		public Bean(Object superior, Object productType, Object salesType, double maxRate, double inventory,
				double salesQuantity) {
			super();
			this.superior = (String) superior;
			this.productType = (String) productType;
			this.salesType = (String) salesType;
			this.maxRate = maxRate;
			this.inventory = inventory;
			this.salesQuantity = salesQuantity;
		}

		public String superior;
		public String productType;
		public String salesType;
		public double maxRate;//销售存货比
		public double inventory;
		public double salesQuantity;

		public int id = ++GenID;
		static int GenID = 100;
	}

	static class Node {
		Map<String, Node> sons;
		public String name;
		public Object value;

		public String toString() {
			return name + ": " + value;
		}

		public Node(String nam, Object value) {
			this.name = nam;
			this.value = value;
		}

		public Node son(Object key) {
			if (sons == null)
				return null;
			return sons.get(key);
		}

		public Node addSon(String key, Object va) {
			Node son = new Node(key, va);
			if (sons == null)
				sons = new TreeMap<>();
			sons.put(key, son);
			return son;
		}

		public void print(String dent) {
			Ca.log(dent + name);
			if (sons == null)
				return;
			dent += "\t";
			for (String key : sons.keySet()) {
				sons.get(key).print(dent);
			}
		}
	}

	public static void generateRule(int opt) {
		if (opt < 0)
			return;
		//若无(主管|经理|财务)签字 (P|S|C|F)类产品(现货|预售)不能高于库存(0.5|1|5|-1)倍
		Object[][] aa = { { "director", "manager", "CFO" }, { "P", "S", "C", "F" }, { "现货", "预售" } };
		Node root = new Node("maxRate:", null);
		List<List<Object>> list = Exhausters.listOf(true, aa);
		for (int i = 0; i < list.size(); i++) {
			List<Object> lis = list.get(i);
			Object c0 = lis.get(0), c2 = lis.get(2);
			double[] nn = { 0.5, 1, 2, 5, 10 };
			double maxRate = nn[(int) (nn.length * Math.random())];
			if (c2.equals("现货")) {
				maxRate = nn[(int) (Math.random() * 2)];
			} else {
				if (i > 0) {
					if (c0.equals("总经理")) {
						maxRate *= 2;
					}
				}
			}
			maxRate = ((int) (maxRate * 100)) / 100.0;
			lis.add(maxRate);
			//List<String> names = Arrays.asList("superior", "productType", "salesType", "maxRate");
			{

			}
			{
				Map<String, String> map = new TreeMap<>();
				String st;
				st = lis.get(0).toString();
				map.put("superior: " + st, st);
				st = lis.get(1).toString();
				map.put("productType: " + st, st);
				st = lis.get(2).toString();
				map.put("salesType: " + st, st);
				//
				st = "@{inventory}";
				map.put("inventory: " + st, st);
				st = "@{salesQuantity}";
				map.put("salesQuantity: " + st, st);
				Node node = root;
				for (String key : map.keySet()) {
					Node son = node.son(key);
					if (son == null) {
						son = node.addSon(key, map.get(key));
					}
					node = son;
				}
				node.addSon("maxRate:" + maxRate, "");

			}
		}
		root.print("");
	}

	public static void generateData(int opt) {
		if (opt < 0)
			return;
		//若无(主管|经理|财务)签字 (P|S|C|F)类产品(现货|预售)不能高于库存(0.5|1|5|-1)倍
		Object[][] aa = { { "主管", "总经理", "财务" }, { "P", "S", "C", "F" }, { "现货", "预售" } };
		List<List<Object>> list = Exhausters.listOf(true, aa);
		Collections.shuffle(list);
		for (int i = 0; i < 200; i++) {
			Bean bean = new Bean();
			/*
			public String superior;
			public String productType;
			public String salesType;
			public double maxRate;//销售存货比
			public double inventory;
			public double salesQuantity;
			 */
			bean.superior = aa[0][(int) (aa[0].length * Math.random())].toString();
			bean.productType = aa[1][(int) (aa[1].length * Math.random())].toString();
			bean.salesType = aa[2][(int) (aa[2].length * Math.random())].toString();
			bean.inventory = (int) (1000 * Math.random());
			if (bean.salesType.equals("现货")) {
				bean.salesQuantity = (double) (bean.inventory * Math.random());
			} else {
				bean.salesQuantity = (double) (1000 * Math.random());
			}
			String xml = Bean9.xmlStrOfBean("Item", bean, "id");
			Ca.log(xml);
			//break;
		}

	}

	public static void main(String[] args) {
		generateRule(1);
		generateData(-1);
	}

}
